package client

import (
	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/openstack"
	"github.com/sirupsen/logrus"
	"openstack_api/config"
)

func GetOrchestrationClient() *gophercloud.ServiceClient {
	c := config.GetOpenstackConfig()
	provider := getToken(c)
	client, err := openstack.NewOrchestrationV1(provider, gophercloud.EndpointOpts{Region: c.Region})
	if err != nil {
		logrus.Error("Get Heat client error,", err)
	}
	return client
}

func getToken(conf config.OpenstackLogin) *gophercloud.ProviderClient {
	opts := gophercloud.AuthOptions{
		IdentityEndpoint: conf.Endpoint,
		Username:         conf.Username,
		Password:         conf.Password,
		DomainName:       conf.Domain,
		TenantName:       conf.TenantName,
	}
	//opts, err := instance.AuthOptionsFromEnv()
	provider, err := openstack.AuthenticatedClient(opts)
	if err != nil {
		logrus.Error("Get openstack token error,", err)
	}
	return provider
}

func GetImageClient() *gophercloud.ServiceClient {
	c := config.GetOpenstackConfig()
	provider := getToken(c)
	client, err := openstack.NewImageServiceV2(provider, gophercloud.EndpointOpts{Region: c.Region})
	if err != nil {
		logrus.Error("Get Image client error,", err)
	}
	return client
}

func GetServerClient() *gophercloud.ServiceClient {
	c := config.GetOpenstackConfig()
	provider := getToken(c)
	client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{Region: c.Region})
	if err != nil {
		logrus.Error("Get Server client error,", err)
	}
	return client
}

func GetVolumeClient() *gophercloud.ServiceClient {
	c := config.GetOpenstackConfig()
	provider := getToken(c)
	client, err := openstack.NewBlockStorageV3(provider, gophercloud.EndpointOpts{Region: c.Region})
	if err != nil {
		logrus.Error("Get Server client error,", err)
	}
	return client
}

//func GetGnocchiClient() *gophercloud.ServiceClient {
//	c := config.GetOpenstackConfig()
//	provider := getToken(c)
//	client, err := gnocchi.NewGnocchiV1(provider, gophercloud.EndpointOpts{Region: c.Region})
//	if err != nil {
//		logrus.Error("Get Server client error,", err)
//	}
//	return client
//}
